package com.moonshineproject.plugin.actionscript.as3project.settings
{
	import __AS3__.vec.Vector;
	
	import com.moonshineproject.plugin.settings.vo.AbstractSetting;
	
	import flash.filesystem.File;
	
	import mx.collections.ArrayCollection;
	import mx.core.IVisualElement;
	
	public class PathListSetting extends AbstractSetting
	{
		protected var copiedPaths:ArrayCollection;
		
		public var allowFiles:Boolean;
		public var allowFolders:Boolean;
		public var fileMustExist:Boolean;
		public var relativeRoot:File;
		
		public function PathListSetting(provider:Object, name:String, label:String, 
										relativeRoot:File=null,
										allowFiles:Boolean=true,
										allowFolders:Boolean=true,
										fileMustExist:Boolean=true)
		{
			super();
			this.provider = provider;
			this.name = name;
			this.label = label;
			this.allowFiles = allowFiles;
			this.allowFolders = allowFolders;
			this.fileMustExist = fileMustExist;
			this.relativeRoot = relativeRoot;
			defaultValue = "";
		}
		
		override public function set stringValue(v:String):void 
		{
			if (v != "")
			{
				var toRet:Vector.<File> = new Vector.<File>();
				var values:Array = v.split(",");
				for each (var v:String in values)
				{
					toRet.push( new File(v) );
				}
			}
			setPendingSetting(toRet);
		}
		
		override public function get renderer():IVisualElement
		{
			var rdr:PathListSettingRenderer = new PathListSettingRenderer();
			rdr.setting = this;
			return rdr;
		}
		
		public function get paths():ArrayCollection
		{
			if (!copiedPaths)
			{
				if (getSetting() == null) return null;
				
				copiedPaths = new ArrayCollection();
				for each (var f:File in getSetting())
				{
					copiedPaths.addItem( 
						new PathListItemVO(new File(f.nativePath), getLabelFor(f))
					);	
				}
			}
			return copiedPaths;
		}
		
		override public function valueChanged():Boolean
        {
        	if (!copiedPaths) return false;
        	
        	var matches:Boolean = true;
        	var itemMatch:Boolean;
        	for each (var f1:File in getSetting())
        	{
        		itemMatch = false;
        		for each (var item:PathListItemVO in copiedPaths)
        		{
        			if (f1.nativePath == item.file.nativePath)
        			{
        				itemMatch = true;
        			}
        		}
        		
        		if (!itemMatch)
        		{
        			matches = false;
        			break;
        		}
        	}
        	
        	// Length mismatch?
        	if (getSetting() && copiedPaths)
        	{
        		if (getSetting().length != copiedPaths.length)
        		{
        			matches = false;	
        		}
        	}
        	
        	return !matches;
        }
        
        override public function commitChanges():void
		{
			if (!hasProperty() || !valueChanged()) return;
			
			var pending:Vector.<File> = new Vector.<File>();
			for each (var item:PathListItemVO in copiedPaths)
			{
				pending.push(item.file);
			}
			
			provider[name] = pending;
			hasPendingChanges = false;
		}
        
		// Helper function
		public function getLabelFor(file:File):String
		{
			var lbl:String;
			if (relativeRoot) lbl = relativeRoot.getRelativePath(file);
			if (relativeRoot && relativeRoot.nativePath == file.nativePath) lbl = "/";
			if (!lbl) lbl = file.nativePath;
			
			if (file.isDirectory
				&& lbl.charAt(lbl.length-1) != "/")
			{
				lbl += "/";	
			}
			
			
			return lbl;
		}        

	}
}